package com.hipac.qs.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.quartz.SchedulerException;

/**
 * Created by xiaogu on 2017/3/8.
 */
public class MyJobListener implements JobListener {
    @Override
    public String getName() {
        return "MyJobListener";
    }

    // 任务即将开始 Trriger 即将点火的时候
    @Override
    public void jobToBeExecuted(JobExecutionContext context) {
        System.out.println("task will executed!!!");
    }

    // 用于在一个即将执行的任务 Trriger 已经点火完毕 却被这个任务的 TriggerListener 否决执行了
    @Override
    public void jobExecutionVetoed(JobExecutionContext context) {
        System.out.println("i'm vetoed by TriggerListener");
    }

    // 任务已经开始执行了 Trriger 已经点火完毕的时候
    @Override
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        System.out.println("task is executeing!!!");
        if(jobException != null){
            try {
                //停止Scheduler
                context.getScheduler().shutdown();
                System.out.println(" Error occurs when executing jobs, shut down the scheduler ");
                // 给管理员发送邮件…
                System.out.println("i am sending an email to admin!!!");
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }
}
